<!--用户管理-->
<template>
  <div>
    <el-form :inline="true">
      <el-input
          v-model='searchForm.name'
          placeholder="名称"
          clearable
      >
      </el-input>
      <el-form-item>
        <el-button @click="">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="insertUser">新增</el-button>
      </el-form-item>
      <!--      <el-form-item>-->
      <!--        <el-popconfirm title="是否确认批量删除?" @confirm="deleteUser(null)">-->
      <!--          <el-button type="danger" slot="reference" :disabled="batchDelete">批量删除</el-button>-->
      <!--        </el-popconfirm>-->
      <!--      </el-form-item>-->
    </el-form>

    <!--数据表格-->
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="img"
          label="展示图"
          width="200"
          align="center">
        <template slot-scope="scope">
          <el-image class="banner_img" size="medium" :src="scope.row.img"></el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="name"
          label="名称"
          width="150"
          align="center">
      </el-table-column>
      <el-table-column
          prop="link"
          label="链接"
          width="500"
          align="center">
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态"
          width="120"
          align="center">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.status === 0" type="danger">未发布</el-tag>
          <el-tag size="small" v-if="scope.row.status === 1">已发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="220"
          align="center">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="修改时间"
          width="220"
          align="center">
      </el-table-column>
      <el-table-column
          label="操作"
          align="center">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.status === 0" @click="issue(scope.row.id)">发布</el-button>
          <el-button type="text" v-if="scope.row.status === 1" @click="notIssue(scope.row.id)">下架</el-button>
          <el-divider direction="vertical" v-if="scope.row.status === 0"></el-divider>
          <el-button type="text" @click="getScreenInfo(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="deleteScreen(scope.row.id)">
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[3, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>

    <!--新增、修改-->
    <el-dialog
        title="显示信息"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <el-form :model="insertFrom" :rules="rules" ref="insertFrom" label-width="120px" class="demo-insertFrom">
        <el-form-item label="名称" prop="name">
          <el-input v-model="insertFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <el-input v-model="insertFrom.img"></el-input>
        </el-form-item>
        <el-form-item label="链接" prop="link">
          <el-input v-model="insertFrom.link"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('insertFrom')">确定</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


  </div>
</template>

<script>
export default {
  name: "Screen",
  data() {
    return {
      searchForm: {},
      insertFrom: {},
      tableData: [],
      roleForm: {},
      total: 0,
      size: 3,
      current: 1,
      roleData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      roleDialogVisible: false,
      batchDelete: true,
      multipleSelection: [],
      dialogVisible: false,
      restPassDialogVisible: false,
      rules: {
        name: [
          {required: true, message: '名称', trigger: 'blur'}
        ],
        img: [
          {required: true, message: '请输入图片', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getScreenList()
  },
  methods: {

    notIssue(id) {
      this.$axios.post('/screen/ms/notIssue/' + id).then(res => {
        this.getScreenList()
        this.$message({
          showClose: true,
          message: res.data.data,
          type: 'success'
        })
      })
    },
    issue(id) {
      this.$axios.post('/screen/ms/issue/' + id).then(res => {
        this.getScreenList()
        this.$message({
          showClose: true,
          message: res.data.data,
          type: 'success'
        })
      })
    }
    ,
    //获取显示列表
    getScreenList() {
      this.$axios.get('/screen/ms/getScreenList', {
        params: {
          name: this.searchForm.name,
          size: this.size,
          current: this.current
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.total = res.data.data.total
        this.current = res.data.data.current
      })
    },
    //新增弹窗
    insertUser() {
      this.dialogVisible = true
    },
    deleteScreen(id) {
      this.$axios.post('/screen/ms/deleteScreen/' + id).then(res => {
        this.getScreenList()
        this.$message({
          showClose: true,
          message: res.data.data,
          type: 'success'
        })
      })
    }
    ,
    submitAllotRole(formName) {
      var roleIds = this.$refs.roleData.getCheckedKeys()
      this.$axios.post('/sys/user/submitAllotRole/', this.roleForm.id, roleIds).then(res => {
        this.$message({
          showClose: true,
          message: res.data.data,
          type: 'success',
          onClose: () => {
            this.getScreenList()
          }
        })
        this.roleDialogVisible = false
      })
    },
    //获取显示信息
    getScreenInfo(id) {
      this.$axios.get('/screen/ms/getScreenInfo/' + id).then(res => {
        this.insertFrom = res.data.data
        this.dialogVisible = true
      })

    },
    //新增或修改信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/screen/ms/' + (this.insertFrom.id ? 'updateScreen' : 'addScreen'), this.insertFrom)
              .then(res => {
                this.getScreenList()
                //成功提交后的弹窗
                this.$message({
                  showClose: true,
                  message: res.data.data,
                  type: 'success'
                });
                this.dialogVisible = false
              })
        } else {
          console.log('提交错误')
          return false
        }
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.batchDelete = val.length == 0
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getScreenList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getScreenList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
      this.insertFrom = {}
    },
    handleClose() {
      this.resetForm('insertFrom')
    }
  }
}
</script>

<style scoped>
.el-input {
  width: 300px;
}

.banner_img {
  width: 100px;
  height: 178px;
}

.el-table-column {
  text-align: center;
}

.el-button {
  margin-left: 10px;
}

.el-pagination {
  margin-top: 10px;
}
</style>